package com.hskn.hss.module.dlbjviolation.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.annotation.DataScope;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.page.TableDataInfo;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.dlbjviolation.entity.DlbjViolation;
import com.hskn.hss.module.dlbjviolation.entity.DlbjViolationCharts;
import com.hskn.hss.module.dlbjviolation.mapper.DlbjViolationMapper;
import com.hskn.hss.module.dlbjviolation.service.IDlbjViolationService;
import com.hskn.hss.module.dlbjviolation.vo.DlbjViolationTableDetailVO;
import com.hskn.hss.module.messageFlowRecords.domain.MessageFlowRecords;
import com.hskn.hss.module.messageFlowRecords.service.IMessageFlowRecordsService;
import com.hskn.hss.module.tlemployee.entity.TlEmployee;
import com.hskn.hss.module.tlemployee.service.ITlEmployeeService;
import com.hskn.hss.module.user.entity.User;
import com.hskn.hss.module.user.mapper.UserMapper;
import com.hskn.hss.module.violationHandleLog.domain.ViolationHandleLog;
import com.hskn.hss.module.violationHandleLog.service.IViolationHandleLogService;
import com.hskn.hss.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-17
 */
@Service
public class DlbjViolationServiceImpl extends ServiceImpl<DlbjViolationMapper, DlbjViolation> implements IDlbjViolationService {

    @Autowired
    public IMessageFlowRecordsService messageFlowRecordsService;
    @Autowired
    LocationUtils locationUtil;
    @Resource
    private DlbjViolationMapper dlbjViolationMapper;
    @Autowired
    private IViolationHandleLogService violationHandleLogService;
    @Resource
    private UserMapper userMapper;
    @Autowired
    private ITlEmployeeService employeeService;

    @Override
    public void mUpdate(JSONObject params) throws HssException {

        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("dlbjViolationId"))) {
            throw new HssException("参数错误");
        }
        DlbjViolation dlbjViolation = JSON.parseObject(params.toJSONString(), DlbjViolation.class);
        dlbjViolationMapper.updateById(dlbjViolation);
    }

    @Override
    public void add(JSONObject params) throws HssException {
        
        DlbjViolation dlbjViolation = JSON.parseObject(params.toJSONString(), DlbjViolation.class);
        dlbjViolationMapper.insert(dlbjViolation);
    }

    @Override
    @DataScope(deptAlias = "depid", userAlias = "person_id, deal_uid")
    public JSONObject mList(BaseEntity entity, JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        JSONObject json = new JSONObject();
        QueryWrapper<DlbjViolation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(DlbjViolation::getCreateTime);
        if (entity.getParams().containsKey("dataScope") && StringUtils.isNotEmpty(entity.getParams().get("dataScope"))) {
            queryWrapper.lambda().apply(entity.getParams().get("dataScope").toString());
        }
        String type = params.getString("type");
        if (StringUtils.isNotEmpty(type)) {
            queryWrapper.lambda().eq(DlbjViolation::getPersonTypeName, type);
        }
        String personTypeId = params.getString("personTypeId");
        if (StringUtils.isNotEmpty(personTypeId)) {
            queryWrapper.lambda().eq(DlbjViolation::getPersonTypeId, personTypeId);
        }
        String dlbjViolationId = params.getString("dlbjViolationId");
        if (StringUtils.isNotEmpty(dlbjViolationId)) {
            queryWrapper.lambda().eq(DlbjViolation::getDlbjViolationId, dlbjViolationId);
        }
        String personTypeName = params.getString("personTypeName");
        if (StringUtils.isNotEmpty(personTypeName)) {
            queryWrapper.lambda().like(DlbjViolation::getPersonTypeName, personTypeName);
        }
        String personId = params.getString("personId");
        if (StringUtils.isNotEmpty(personId)) {
            queryWrapper.lambda().eq(DlbjViolation::getPersonId, personId);
        }
        String violationTypeId = params.getString("violationTypeId");
        if (StringUtils.isNotEmpty(violationTypeId)) {
            queryWrapper.lambda().eq(DlbjViolation::getViolationTypeId, violationTypeId);
        }
        String violationTypeName = params.getString("violationTypeName");
        if (StringUtils.isNotEmpty(violationTypeName)) {
            queryWrapper.lambda().eq(DlbjViolation::getViolationTypeName, violationTypeName);
        }
        String startDate = AttributeUtils.timeCompletion(params.getString("startDate"), 's');
        String endDate = AttributeUtils.timeCompletion(params.getString("endDate"), 'e');
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            queryWrapper.lambda().between(DlbjViolation::getDate, startDate, endDate);
        }
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        Page<DlbjViolation> attendPage = new Page<>(page, size, true);
        IPage<DlbjViolation> iPage = dlbjViolationMapper.selectPage(attendPage, queryWrapper);
        List<DlbjViolation> list = iPage.getRecords();
        Map<String, TlEmployee> tlEmployeesMap = employeeService.getEmpMap(new QueryWrapper<>());
        for (DlbjViolation dlbjViolation : list) {
            if (tlEmployeesMap.containsKey(dlbjViolation.getPersonId())) {
                dlbjViolation.setEmpHeadPortraitViolation(tlEmployeesMap.get(dlbjViolation.getPersonId()).getPersonphoto());
            }
            MessageFlowRecords messageFlowRecords = new MessageFlowRecords();
            messageFlowRecords.setMessageId(dlbjViolation.getDlbjViolationId());
            messageFlowRecords.setKind(2);
            messageFlowRecords.setSubjectType(1);
            messageFlowRecords.setUserId(uid);
            List<MessageFlowRecords> messageFlowRecords1 = messageFlowRecordsService.selectMessageFlowRecordsList(messageFlowRecords);
            if ((Objects.nonNull(messageFlowRecords1) && messageFlowRecords1.size() > 0) || dlbjViolation.getDate().compareTo(DateUtil.parseDateTime(AttributeUtils.timeCompletion(DateUtil.formatDate(new Date()), 's'))) < 0) {
                dlbjViolation.setIsRead(1);
            } else {
                dlbjViolation.setIsRead(0);
            }
        }
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("list", list);
        return json;
    }

    @Override
    public JSONObject collectList(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        JSONObject res = new JSONObject();
        Long page = (StringUtils.isEmpty(params.getLong("page")) || params.getLong("page") == 0) ? 1 : params.getLong("page");
        Long size = (StringUtils.isEmpty(params.getLong("size")) || params.getLong("size") == 0) ? 10 : params.getLong("size");
        params.put("offset", (page - 1) * size);
        params.put("limit", size);
        String date = String.valueOf(params.get("date"));
        if (StringUtils.isNotEmpty(date)) {
            String startDate = AttributeUtils.timeCompletion(date, 's');
            String endDate = AttributeUtils.timeCompletion(date, 'e');

            params.put("startDate", startDate);
            params.put("endDate", endDate);
        }
        List<Map<String, Object>> list = dlbjViolationMapper.collectList(params);
        long count = dlbjViolationMapper.collectListCount(params);
        res.put("list", list);
        res.put("total", count);
        res.put("current", page);
        res.put("pages", (int) Math.ceil(count / size));
        res.put("size", list.size());
        return res;
    }

    @Override
    public List<Map<String, Object>> histogram(JSONObject params) {

        QueryWrapper<DlbjViolation> queryWrapper = new QueryWrapper<>();
        String personTypeId = params.getString("personTypeId");
        if (StringUtils.isNotEmpty(personTypeId)) {
            queryWrapper.lambda().eq(DlbjViolation::getPersonTypeId, personTypeId);
        }
        String personTypeName = params.getString("personTypeName");
        if (StringUtils.isNotEmpty(personTypeName)) {
            queryWrapper.lambda().like(DlbjViolation::getPersonTypeName, personTypeName);
        }
        String personId = params.getString("personId");
        if (StringUtils.isNotEmpty(personId)) {
            queryWrapper.lambda().eq(DlbjViolation::getPersonId, personId);
        }
        String violationTypeId = params.getString("violationTypeId");
        if (StringUtils.isNotEmpty(violationTypeId)) {
            queryWrapper.lambda().eq(DlbjViolation::getViolationTypeId, violationTypeId);
        }
        String violationTypeName = params.getString("violationTypeName");
        if (StringUtils.isNotEmpty(violationTypeName)) {
            queryWrapper.lambda().eq(DlbjViolation::getViolationTypeName, violationTypeName);
        }
        String startDate = params.getString("startDate");
        String endDate = params.getString("endDate");
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            queryWrapper.lambda().between(DlbjViolation::getDate, startDate, endDate);
        }
        queryWrapper.select("person_id as personId, (select dv1.person_name from dlbj_violation dv1 where dv1.person_id = person_id limit 1) as 'personName', count(1) as count");
        queryWrapper.lambda().groupBy(DlbjViolation::getPersonId);
        List<Map<String, Object>> list = listMaps(queryWrapper);
        return list;
    }

    @Override
    public void deal(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("dlbjViolationIds"))) {
            throw new HssException("参数错误");
        }
        String dlbjViolationIdStr = params.getString("dlbjViolationIds");
        String[] dlbjViolationIds = dlbjViolationIdStr.split(",");
        QueryWrapper<DlbjViolation> dlbjViolationQueryWrapper = new QueryWrapper<>();
        dlbjViolationQueryWrapper.lambda().in(DlbjViolation::getDlbjViolationId, dlbjViolationIds);
        List<DlbjViolation> dlbjViolationList = dlbjViolationMapper.selectList(dlbjViolationQueryWrapper);
        Map<String, DlbjViolation> dlbjViolationMap = null;
        if (!dlbjViolationList.isEmpty()) {
            dlbjViolationMap = GroupingByUtils.getGroupingForMapEntity(dlbjViolationList,
                    DlbjViolation::getDlbjViolationId, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        } else {
            dlbjViolationMap = new HashMap<>();
        }
        User user = userMapper.selectById(uid);
        List<DlbjViolation> dlbjViolations = new ArrayList<>();
        DlbjViolation dlbjViolation = null;
        List<ViolationHandleLog> violationHandleLogList = new LinkedList<>();
        ViolationHandleLog violationHandleLog = null;
        DlbjViolation violation = null;
        for (String dlbjViolationId : dlbjViolationIds) {
            dlbjViolation = new DlbjViolation(dlbjViolationId, params.getString("dealType"), uid, params.getString("dealRemark"));
            dlbjViolations.add(dlbjViolation);
            if (dlbjViolationMap.containsKey(dlbjViolationId)) {
                violation = dlbjViolationMap.get(dlbjViolationId);
                violationHandleLog = getViolationHandleLog(violation, params, uid, user);
                violationHandleLogList.add(violationHandleLog);
            }
        }
        violationHandleLogService.saveBatch(violationHandleLogList);
        updateBatchById(dlbjViolations);
    }

    private ViolationHandleLog getViolationHandleLog(DlbjViolation violation, JSONObject params, String uid, User user) {
        ViolationHandleLog violationHandleLog = new ViolationHandleLog(
                StringUtils.uuid(), violation.getDlbjViolationId(), violation.getPersonId(), violation.getPersonName(), violation.getPersonTypeId(), violation.getPersonTypeName(), new Date(), violation.getDepid()
                , violation.getDepName(), violation.getViolationTypeId(), violation.getViolationTypeName(), params.getString("dealType"), uid,
                user != null ? user.getName() : "", params.getString("dealRemark"), violation.getContent(), violation.getViolationStartTime(),
                violation.getViolationEndTime(), violation.getIsKeep(), violation.getLng(), violation.getLat(), violation.getDate(), violation.getAddress()
                , violation.getDuration(), "emp"
        );
        return violationHandleLog;
    }

    @Override
    public Map<String, Object> collectCount(JSONObject params) throws HssException {

        String date = String.valueOf(params.get("date"));
        if (StringUtils.isNotEmpty(date)) {
            String startDate = AttributeUtils.timeCompletion(date, 's');
            String endDate = AttributeUtils.timeCompletion(date, 'e');

            params.put("startDate", startDate);
            params.put("endDate", endDate);
        }
        Map<String, Object> count = dlbjViolationMapper.collectCount(params);
        return count;
    }


    @Override
    public List<List<Object>> selectDlbjViolationForCharts(DlbjViolation dlbjViolation) throws ParseException {
        if (Objects.isNull(dlbjViolation.getViolationStartTime())) {
            dlbjViolation.setViolationStartTime(DateUtil.parseDateTime(DateUtil.format(new Date(), DatePattern.NORM_MONTH_PATTERN) + "-01 00:00:00"));
        } else {
            dlbjViolation.setViolationStartTime(DateUtil.parseDateTime(DateUtil.formatDate(dlbjViolation.getViolationStartTime()) + " 00:00:00"));
        }
        if (Objects.isNull(dlbjViolation.getViolationEndTime())) {
            dlbjViolation.setViolationEndTime(DateUtil.parseDateTime(DateUtil.formatDate(new Date()) + " 23:59:59"));
        } else {
            dlbjViolation.setViolationEndTime(DateUtil.parseDateTime(DateUtil.formatDate(dlbjViolation.getViolationEndTime()) + " 23:59:59"));
        }
        Date startDate = dlbjViolation.getViolationStartTime();
        Date endDate = dlbjViolation.getViolationEndTime();
        List<DlbjViolationCharts> bTableList = dlbjViolationMapper.selectDlbjViolationForCharts(dlbjViolation);

        if (Objects.nonNull(bTableList) && bTableList.size() > 0) {
            QueryWrapper<DlbjViolation> dlbjViolationQueryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(dlbjViolation.getPersonId())) {
                dlbjViolationQueryWrapper.lambda().eq(DlbjViolation::getPersonId, dlbjViolation.getPersonId());
            }
            if (StringUtils.isNotEmpty(dlbjViolation.getPersonName())) {
                dlbjViolationQueryWrapper.lambda().eq(DlbjViolation::getPersonName, dlbjViolation.getPersonName());
            }
            if (StringUtils.isNotEmpty(dlbjViolation.getViolationTypeName())) {
                dlbjViolationQueryWrapper.lambda().eq(DlbjViolation::getViolationTypeName, dlbjViolation.getViolationTypeName());
            }
            if ("2".equals(dlbjViolation.getDateType())) {
                dlbjViolationQueryWrapper.lambda().ge(DlbjViolation::getDate, bTableList.get(0).getDate() + "-01 00:00:00");
                dlbjViolationQueryWrapper.lambda().le(DlbjViolation::getDate, bTableList.get(bTableList.size() - 1).getDate() + "-31 23:59:59");
            } else {
                dlbjViolationQueryWrapper.lambda().ge(DlbjViolation::getDate, bTableList.get(0).getDate() + " 00:00:00");
                dlbjViolationQueryWrapper.lambda().le(DlbjViolation::getDate, bTableList.get(bTableList.size() - 1).getDate() + " 23:59:59");
            }

            List<DlbjViolation> dlbjViolationList = dlbjViolationMapper.selectList(dlbjViolationQueryWrapper);

            if ("2".equals(dlbjViolation.getDateType())) {
                dlbjViolationList.forEach(t -> {
                    t.setDay(DateUtil.format(t.getDate(), DatePattern.NORM_MONTH_PATTERN));
                });
            } else {
                dlbjViolationList.forEach(t -> {
                    t.setDay(DateUtil.formatDate(t.getDate()));
                });
            }

            Map<String, List<DlbjViolation>> dlbjViolationMap = dlbjViolationList.stream().collect(Collectors.groupingBy(DlbjViolation::getDay));

            Map<String, Map<String, List<DlbjViolation>>> map2 = new HashMap<>();
            dlbjViolationMap.keySet().forEach(t -> {
                List<DlbjViolation> dlbjViolations = dlbjViolationMap.get(t);
                Map<String, List<DlbjViolation>> alarmTypeDlbjMap = dlbjViolations.stream().collect(Collectors.groupingBy(DlbjViolation::getViolationTypeName));
                map2.put(dlbjViolations.get(0).getDay(), alarmTypeDlbjMap);
            });

            for (DlbjViolationCharts dlbjViolationCharts : bTableList
            ) {
                if (Objects.nonNull(map2) && map2.size() > 0) {
                    if (Objects.nonNull(map2.get(dlbjViolationCharts.getDate()).get("停留"))) {
                        dlbjViolationCharts.setTlCount(map2.get(dlbjViolationCharts.getDate()).get("停留").size());
                    }
                    if (Objects.nonNull(map2.get(dlbjViolationCharts.getDate()).get("离岗"))) {
                        dlbjViolationCharts.setLgCount(map2.get(dlbjViolationCharts.getDate()).get("离岗").size());
                    }
                    if (Objects.nonNull(map2.get(dlbjViolationCharts.getDate()).get("聚众"))) {
                        dlbjViolationCharts.setJzCount(map2.get(dlbjViolationCharts.getDate()).get("聚众").size());
                    }
                }
            }
        }
        if ("2".equals(dlbjViolation.getDateType())) {
            return getSelectAlarmForChartsByMonth(startDate, endDate, bTableList);
        } else {
            return getSelectAlarmForCharts(startDate, endDate, bTableList);
        }
    }

    @Override
    public List<DlbjViolationCharts> selectDlbjViolationForTable(DlbjViolation dlbjViolation) {
        if (Objects.isNull(dlbjViolation.getViolationStartTime())) {
            dlbjViolation.setViolationStartTime(DateUtil.parseDateTime(DateUtil.format(new Date(), DatePattern.NORM_MONTH_PATTERN) + "-01 00:00:00"));
        } else {
            dlbjViolation.setViolationStartTime(DateUtil.parseDateTime(DateUtil.formatDate(dlbjViolation.getViolationStartTime()) + " 00:00:00"));
        }
        if (Objects.isNull(dlbjViolation.getViolationEndTime())) {
            dlbjViolation.setViolationEndTime(DateUtil.parseDateTime(DateUtil.formatDate(new Date()) + " 23:59:59"));
        } else {
            dlbjViolation.setViolationEndTime(DateUtil.parseDateTime(DateUtil.formatDate(dlbjViolation.getViolationEndTime()) + " 23:59:59"));
        }
        List<DlbjViolationCharts> dlbjViolationChartsList = dlbjViolationMapper.selectDlbjViolationForTable(dlbjViolation);
        if (Objects.nonNull(dlbjViolationChartsList) && dlbjViolationChartsList.size() > 0) {
            QueryWrapper<DlbjViolation> dlbjViolationQueryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(dlbjViolation.getPersonId())) {
                dlbjViolationQueryWrapper.lambda().eq(DlbjViolation::getPersonId, dlbjViolation.getPersonId());
            }
            if (StringUtils.isNotEmpty(dlbjViolation.getPersonName())) {
                dlbjViolationQueryWrapper.lambda().eq(DlbjViolation::getPersonName, dlbjViolation.getPersonName());
            }
            if (StringUtils.isNotEmpty(dlbjViolation.getViolationTypeName())) {
                dlbjViolationQueryWrapper.lambda().eq(DlbjViolation::getViolationTypeName, dlbjViolation.getViolationTypeName());
            }

            dlbjViolationQueryWrapper.lambda().ge(DlbjViolation::getDate, dlbjViolation.getViolationStartTime());
            dlbjViolationQueryWrapper.lambda().le(DlbjViolation::getDate, dlbjViolation.getViolationEndTime());

            List<DlbjViolation> dlbjViolationList = dlbjViolationMapper.selectList(dlbjViolationQueryWrapper);

            dlbjViolationList.forEach(t -> {
                t.setDay(DateUtil.formatDate(t.getDate()));
            });
            Map<String, List<DlbjViolation>> dlbjViolationMap = dlbjViolationList.stream().collect(Collectors.groupingBy(DlbjViolation::getPersonName));

            Map<String, Map<String, List<DlbjViolation>>> map2 = new HashMap<>();
            dlbjViolationMap.keySet().forEach(t -> {
                List<DlbjViolation> dlbjViolations = dlbjViolationMap.get(t);
                Map<String, List<DlbjViolation>> alarmTypeDlbjMap = dlbjViolations.stream().collect(Collectors.groupingBy(DlbjViolation::getViolationTypeName));
                map2.put(dlbjViolations.get(0).getPersonName(), alarmTypeDlbjMap);
            });

            for (DlbjViolationCharts dlbjViolationCharts : dlbjViolationChartsList
            ) {
                if (Objects.nonNull(map2.get(dlbjViolationCharts.getPersonName()).get("停留"))) {
                    dlbjViolationCharts.setTlCount(map2.get(dlbjViolationCharts.getPersonName()).get("停留").size());
                }
                if (Objects.nonNull(map2.get(dlbjViolationCharts.getPersonName()).get("离岗"))) {
                    dlbjViolationCharts.setLgCount(map2.get(dlbjViolationCharts.getPersonName()).get("离岗").size());
                }
                if (Objects.nonNull(map2.get(dlbjViolationCharts.getPersonName()).get("聚众"))) {
                    dlbjViolationCharts.setJzCount(map2.get(dlbjViolationCharts.getPersonName()).get("聚众").size());
                }
            }
        }
        return dlbjViolationChartsList;
    }


    private List<List<Object>> getSelectAlarmForCharts(Date startDate, Date endDate, List<DlbjViolationCharts> bTableList) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<Object> resList = new ArrayList<>();
        List<List<Object>> resList2 = new ArrayList<>();
        List<String> dateStringList = new ArrayList<>();
        dateStringList = Tools.getDateList(format, startDate, endDate);
        Boolean flag = true;
        for (int i = 0; i < dateStringList.size(); i++) {
            resList.add(dateStringList.get(i));
            for (DlbjViolationCharts c : bTableList) {
                if (dateStringList.get(i).equals(c.getDate())) {
                    flag = false;
                    resList.addAll(c.getResList());
                    resList2.add(resList);
                }
            }
            for (DlbjViolationCharts c : bTableList) {
                if (flag) {
                    resList.add(0);
                    resList.add(0);
                    resList.add(0);
                    resList.add(0);
                    resList2.add(resList);
                    flag = false;
                }
            }

            resList = new ArrayList<>();
            flag = true;

        }
        return resList2;
    }

    private List<List<Object>> getSelectAlarmForChartsByMonth(Date startDate, Date endDate, List<DlbjViolationCharts> bTableList) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        List<Object> resList = new ArrayList<>();
        List<List<Object>> resList2 = new ArrayList<>();
        List<String> dateStringList = new ArrayList<>();

        String beginDate = DateUtil.formatDate(startDate);
        String lastDate = DateUtil.formatDate(endDate);
        dateStringList = Tools.getMonthList(beginDate, lastDate);
        Boolean flag = true;
        for (int i = 0; i < dateStringList.size(); i++) {
            resList.add(dateStringList.get(i));
            for (DlbjViolationCharts c : bTableList) {
                if (dateStringList.get(i).equals(c.getDate())) {
                    flag = false;
                    resList.addAll(c.getResList());
                    resList2.add(resList);
                }
            }
            for (DlbjViolationCharts c : bTableList) {
                if (flag) {
                    resList.add(0);
                    resList.add(0);
                    resList.add(0);
                    resList.add(0);
                    resList2.add(resList);
                    flag = false;
                }
            }

            resList = new ArrayList<>();
            flag = true;

        }
        return resList2;
    }

    /**
     * 列表明细
     */
    @Override
    public TableDataInfo selectDlbjViolationForTableDetail(DlbjViolation dlbjViolation) {
        if (Objects.isNull(dlbjViolation.getViolationStartTime())) {
            dlbjViolation.setViolationStartTime(DateUtil.parseDateTime(DateUtil.format(new Date(), DatePattern.NORM_MONTH_PATTERN) + "-01 00:00:00"));
        } else {
            dlbjViolation.setViolationStartTime(DateUtil.parseDateTime(DateUtil.formatDate(dlbjViolation.getViolationStartTime()) + " 00:00:00"));
        }
        if (Objects.isNull(dlbjViolation.getViolationEndTime())) {
            dlbjViolation.setViolationEndTime(DateUtil.parseDateTime(DateUtil.formatDate(new Date()) + " 23:59:59"));
        } else {
            dlbjViolation.setViolationEndTime(DateUtil.parseDateTime(DateUtil.formatDate(dlbjViolation.getViolationEndTime()) + " 23:59:59"));
        }
        QueryWrapper<DlbjViolation> dlbjViolationQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(dlbjViolation.getPersonId())) {
            dlbjViolationQueryWrapper.lambda().eq(DlbjViolation::getPersonId, dlbjViolation.getPersonId());
        }
        if (StringUtils.isNotEmpty(dlbjViolation.getPersonName())) {
            dlbjViolationQueryWrapper.lambda().eq(DlbjViolation::getPersonName, dlbjViolation.getPersonName());
        }
        if (StringUtils.isNotEmpty(dlbjViolation.getViolationTypeName())) {
            dlbjViolationQueryWrapper.lambda().eq(DlbjViolation::getViolationTypeName, dlbjViolation.getViolationTypeName());
        }

        dlbjViolationQueryWrapper.lambda().eq(DlbjViolation::getPersonId, dlbjViolation.getPersonId());
        dlbjViolationQueryWrapper.lambda().ge(DlbjViolation::getDate, dlbjViolation.getViolationStartTime());
        dlbjViolationQueryWrapper.lambda().le(DlbjViolation::getDate, dlbjViolation.getViolationEndTime());
        dlbjViolationQueryWrapper.lambda().orderByDesc(DlbjViolation::getDate);

        List<DlbjViolation> dlbjViolationList = dlbjViolationMapper.selectList(dlbjViolationQueryWrapper);

        List<DlbjViolationTableDetailVO> dlbjViolationTableDetailVOList = new ArrayList<>();
        dlbjViolationList.forEach(t -> {
            DlbjViolationTableDetailVO dlbjViolationTableDetailVO = new DlbjViolationTableDetailVO();
            dlbjViolationTableDetailVO.setPersonId(t.getPersonId());
            dlbjViolationTableDetailVO.setPersonName(t.getPersonName());
            dlbjViolationTableDetailVO.setPersonTypeName(t.getPersonTypeName());
            dlbjViolationTableDetailVO.setDepName(t.getDepName());
            dlbjViolationTableDetailVO.setViolationTypeName(t.getViolationTypeName());
            dlbjViolationTableDetailVO.setViolationStartTime(DateUtil.formatDateTime(t.getViolationStartTime()));
            dlbjViolationTableDetailVO.setViolationEndTime(DateUtil.formatDateTime(t.getViolationEndTime()));
            dlbjViolationTableDetailVO.setContent(t.getContent());
            /** 保存地址 **/
            String currentAddress = locationUtil.getAddress(t.getLng(), t.getLat());
            if (StringUtils.isNotEmpty(currentAddress)) {
                dlbjViolationTableDetailVO.setAddress(currentAddress);
            } else {
                dlbjViolationTableDetailVO.setAddress("");
            }
            dlbjViolationTableDetailVOList.add(dlbjViolationTableDetailVO);
        });
        return Tools.buildTableDataInfo(dlbjViolation.getPageNum(), dlbjViolation.getPageSize(), dlbjViolationTableDetailVOList);
    }
}
